package com.qingmeng.applyjob.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.qingmeng.applyjob.mapper.*;
import com.qingmeng.applyjob.model.*;
import com.qingmeng.applyjob.portal.enums.CoachScheduleCode;
import com.qingmeng.applyjob.portal.model.dto.*;
import com.qingmeng.applyjob.portal.model.vo.CoachListVo;
import com.qingmeng.applyjob.portal.model.vo.CoachingRecordListVo;
import com.qingmeng.applyjob.portal.model.vo.MyCoachListVo;
import com.qingmeng.applyjob.portal.model.vo.TeacherVo;
import com.qingmeng.applyjob.portal.model.vo.voclass.Records;
import com.qingmeng.applyjob.portal.model.vo.voclass.RecordsComment;
import com.qingmeng.applyjob.portal.model.vo.voclass.WorkVo;
import com.qingmeng.applyjob.portal.service.CoachService;

import com.qingmeng.applyjob.portal.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CoachServiceImpl implements CoachService {

    private static final Logger logger = LoggerFactory.getLogger(CoachServiceImpl.class);

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserTeacherRelationMapper userTeacherRelationMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private CoachTeacherRelationMapper coachTeacherRelationMapper;
    @Autowired
    private AppointmentCoachingMapper appointmentCoachingMapper;
    @Autowired
    private CoachAppointmentRelationMapper appointmentRelationMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CoachCommentRelationMapper coachCommentRelationMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private CommentReplyRelationMapper commentReplyRelationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int improveTeacherInformation(ImproveTeacherInformationParam improveTeacherInformationParam) {
        Teacher teacher = new Teacher();
        teacher.setNickName(improveTeacherInformationParam.getNickName());
        teacher.setCompany(improveTeacherInformationParam.getTeacherCompany());
        teacher.setWorkDuration(improveTeacherInformationParam.getWorkDuration());
        teacher.setPosition(improveTeacherInformationParam.getPosition());
        teacher.setCoachKey(improveTeacherInformationParam.getTeacherDirection());
        teacher.setAvatar(improveTeacherInformationParam.getAvatar());
        teacher.setCreateTime(new Date());
        if (teacherMapper.insertSelective(teacher) <= 0) {
            return 0;
        } else {
            UserTeacherRelation userTeacherRelation = new UserTeacherRelation();
            userTeacherRelation.setUserId(SecurityUtils.getCurrentUser().getId());
            userTeacherRelation.setTeacherId(teacher.getId());
            if (userTeacherRelationMapper.insertSelective(userTeacherRelation) <= 0) {
                return 0;
            }
        }

        Work work = new Work();
        work.setCompanyName(improveTeacherInformationParam.getWorkCompany());
        work.setPosition(improveTeacherInformationParam.getWorkPosition());
        work.setIntroduce(improveTeacherInformationParam.getWorkIntroduce());
        work.setStartTime(improveTeacherInformationParam.getStartTime());
        work.setEndTime(improveTeacherInformationParam.getEndTime());
        work.setUserId(SecurityUtils.getCurrentUser().getId());
        work.setCreateTime(new Date());
        if (workMapper.insertSelective(work) <= 0) {
            return 0;
        }

        return 1;
    }

    @Transactional
    @Override
    public int publishCoach(CoachPublishParam coachPublishParam) {


//        Teacher teacher = new Teacher();
//        teacher.setNickName(coachPublishParam.getNickName());
//        teacher.setCompany(coachPublishParam.getTeacherCompany());
//        teacher.setWorkDuration(coachPublishParam.getWorkDuration());
//        teacher.setPosition(coachPublishParam.getPosition());
//        teacher.setCoachKey(coachPublishParam.getTeacherDirection());
//        teacher.setAvatar(coachPublishParam.getAvatar());
//        teacher.setCreateTime(new Date());
//        if (teacherMapper.insertSelective(teacher) <= 0){
//            return 0;
//        }else {
//            UserTeacherRelation userTeacherRelation = new UserTeacherRelation();
//            userTeacherRelation.setUserId(SecurityUtils.getCurrentUser().getId());
//            userTeacherRelation.setTeacherId(teacher.getId());
//            if (userTeacherRelationMapper.insertSelective(userTeacherRelation) <= 0) {
//                return 0;
//            }
//        }
//
//        Work work = new Work();
//        work.setCompanyName(coachPublishParam.getWorkCompany());
//        work.setPosition(coachPublishParam.getWorkPosition());
//        work.setIntroduce(coachPublishParam.getWorkIntroduce());
//        work.setStartTime(coachPublishParam.getStartTime());
//        work.setEndTime(coachPublishParam.getEndTime());
//        work.setUserId(SecurityUtils.getCurrentUser().getId());
//        work.setCreateTime(new Date());
//        if (workMapper.insertSelective(work) <= 0){
//            return 0;
//        }

        Coach coach = new Coach();
        coach.setCreateTime(new Date());
        coach.setDirection(coachPublishParam.getDirection());
        coach.setPrice(coachPublishParam.getPrice());
        coach.setCoachTime(coachPublishParam.getCoachTime());
        coach.setCreateTime(new Date());
        coach.setCertification(0);

        if (coachMapper.insertSelective(coach) <= 0) {  //调用这个语句的同时会 先对id进行自增 同时调用 coach.setId(); 对coach对象的id进行赋值
            return 0;
        } else {
            CoachTeacherRelation coachTeacherRelation = new CoachTeacherRelation();
            coachTeacherRelation.setCoachId(coach.getId());

            //先获取到当前用户再获取当前用户对应的老师id
            User currentUser = SecurityUtils.getCurrentUser();
            UserTeacherRelationExample userTeacherRelationExample = new UserTeacherRelationExample();
            userTeacherRelationExample.createCriteria().andUserIdEqualTo(currentUser.getId());
            List<UserTeacherRelation> userTeacherRelations = userTeacherRelationMapper.selectByExample(userTeacherRelationExample);
            Long teacherId = userTeacherRelations.get(0).getTeacherId();

            coachTeacherRelation.setTeacherId(teacherId);
            if (coachTeacherRelationMapper.insertSelective(coachTeacherRelation) <= 0) {
                return 0;
            }
        }

        return 1;
    }

//    @Transactional
//    @Override
//    public List<CoachListVo> list(int pageNum, int pageSize) {
//        List<CoachListVo> coachListVos = new ArrayList<>();
//        //分页插件，对第一条sql语句进行分页
//        PageHelper.startPage(pageNum,pageSize);
//        //根据CoachExample获取数据库里的coach数据
//        CoachExample coachExample = new CoachExample();
//        List<Coach> coachList = coachMapper.selectByExample(coachExample);
//        for (Coach coach : coachList){
//            CoachListVo coachListVo = new CoachListVo();
//            coachListVo.setCoach(coach);
//
//            //根据coach对象获取到teacher id
//            CoachTeacherRelationExample coachTeacherRelationExample = new CoachTeacherRelationExample();
//            coachTeacherRelationExample.createCriteria().andCoachIdEqualTo(coach.getId());
//            List<CoachTeacherRelation> coachTeacherRelations = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample);
//            logger.info("查到的关联表数组是：{}",coachTeacherRelations);
//            Long teacherId = coachTeacherRelations.get(0).getTeacherId();
//            TeacherExample teacherExample = new TeacherExample();
//            teacherExample.createCriteria().andIdEqualTo(teacherId);
//            //根据teacherId获取teacher对象
//            List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
//            coachListVo.setTeacher(teachers.get(0));
//
//            //根据teacher id 获取到user id
//            UserTeacherRelationExample userTeacherRelationExample = new UserTeacherRelationExample();
//            userTeacherRelationExample.createCriteria().andTeacherIdEqualTo(teacherId);
//            List<UserTeacherRelation> userTeacherRelations = userTeacherRelationMapper.selectByExample(userTeacherRelationExample);
//            Long userId = userTeacherRelations.get(0).getUserId();
//            //根据userId 获取 work对象
//            WorkExample workExample = new WorkExample();
//            workExample.createCriteria().andUserIdEqualTo(userId);
//            List<Work> workList = workMapper.selectByExample(workExample);
//            coachListVo.setWorkList(workList);
//
//            coachListVos.add(coachListVo);
//        }
//
//        return coachListVos;
//    }

    @Transactional
    @Override
    public TeacherVo teacherInformation(Long teacherId, Long coachId) {
        TeacherVo teacherVo = new TeacherVo();
        teacherVo.setTeacherId(teacherId);
        //根据teacherId获取teacher相关信息
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andIdEqualTo(teacherId);
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        Teacher teacher = teachers.get(0);
        teacherVo.setNickName(teacher.getNickName());
        teacherVo.setCompany(teacher.getCompany());
        teacherVo.setWorkDuration(teacher.getWorkDuration());
        teacherVo.setPosition(teacher.getPosition());
        teacherVo.setAvatar(teacher.getAvatar());

        //计算出辅导的总次数，以及评分平均值
        //首先根据teacherId获取到coachTeacherRelations列表 再根据coachId进一步获取到comment从而获取评分
        CoachTeacherRelationExample coachTeacherRelationExample = new CoachTeacherRelationExample();
        coachTeacherRelationExample.createCriteria().andTeacherIdEqualTo(teacherId);
        List<CoachTeacherRelation> coachTeacherRelations = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample);
        int all = 0;
        Double score = 0.0;
        for (CoachTeacherRelation coachTeacherRelation : coachTeacherRelations) {
            Long coachId1 = coachTeacherRelation.getCoachId();
            CoachCommentRelationExample coachCommentRelationExample = new CoachCommentRelationExample();
            coachCommentRelationExample.createCriteria().andCoachIdEqualTo(coachId1);
            List<CoachCommentRelation> coachCommentRelations = coachCommentRelationMapper.selectByExample(coachCommentRelationExample);
            if (coachCommentRelations != null && coachCommentRelations.size() > 0) {
                //计算出一共获得的总分数，和一共授课总数，最后计算综合平均分
                // 还没有获得用户评分以及评论的不算完成一次辅导
                Double Sscore = 0.0;
                int Sall = 0;
                for (CoachCommentRelation coachCommentRelation : coachCommentRelations) {
                    CommentExample commentExample = new CommentExample();
                    commentExample.createCriteria().andIdEqualTo(coachCommentRelation.getId());
                    List<Comment> comments = commentMapper.selectByExample(commentExample);
                    Sscore += comments.get(0).getScore();
                    Sall += 1;
                }
//                Sscore = Sscore / Sall;
                score += Sscore;
                all += Sall;

                score = score / all;
                teacherVo.setCoachNumber(all);
                teacherVo.setScore(score);
            }
        }
        //获取工作经验列表
        //根据teacherId获取userId
        //根据userId获取work
        UserTeacherRelationExample userTeacherRelationExample = new UserTeacherRelationExample();
        userTeacherRelationExample.createCriteria().andTeacherIdEqualTo(teacherId);
        List<UserTeacherRelation> userTeacherRelations = userTeacherRelationMapper.selectByExample(userTeacherRelationExample);
        Long userId = userTeacherRelations.get(0).getUserId();
        WorkExample workExample = new WorkExample();
        workExample.createCriteria().andUserIdEqualTo(userId);
        List<Work> works = workMapper.selectByExample(workExample);
        List<WorkVo> workVos = new ArrayList<>();
        if (works != null && works.size() > 0) {
            for (Work work : works) {
                WorkVo workVo = new WorkVo();
                workVo.setWorkIntroduce(work.getIntroduce());
                workVo.setWorkPosition(work.getPosition());
                workVo.setCompanyName(work.getCompanyName());
                if (work.getStartTime() != null) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
                    String format = simpleDateFormat.format(work.getStartTime());
                    workVo.setStartTime(format);
                } else {
                    workVo.setStartTime(null);
                }
                if (work.getEndTime() != null) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
                    String format = simpleDateFormat.format(work.getEndTime());
                    workVo.setEndTime(format);
                } else {
                    workVo.setEndTime("至今");
                }
                workVos.add(workVo);
            }
        }
        teacherVo.setWorkList(workVos);
        //获取辅导侧重点
        CoachExample coachExample = new CoachExample();
        coachExample.createCriteria().andIdEqualTo(coachId);
        List<Coach> coaches = coachMapper.selectByExample(coachExample);
        String direction = coaches.get(0).getDirection();
        teacherVo.setDirection(direction);
        return teacherVo;
    }

    @Transactional
    @Override
    public List<CoachListVo> coachList(int pageNum, int pageSize) {
        List<CoachListVo> coachListVos = new ArrayList<>();
        //根据数据库中所有的总的辅导数进行分页
        PageHelper.startPage(pageNum, pageSize);
        CoachExample coachExample = new CoachExample();
        List<Coach> coaches = coachMapper.selectByExample(coachExample);
        for (Coach coach : coaches) {
            CoachListVo coachListVo = new CoachListVo();
            //存放辅导相关信息
            coachListVo.setCoachId(coach.getId());
            coachListVo.setDirection(coach.getDirection());
            coachListVo.setPrice(coach.getPrice());
            if (coach.getCreateTime() != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
                String format = simpleDateFormat.format(coach.getCreateTime());
                coachListVo.setCreateTime(format);
            } else {
                coachListVo.setCreateTime(null);
            }

            coachListVo.setCertification(coach.getCertification());
            //根据coachId获取到teacher表相关信息并存放
            CoachTeacherRelationExample coachTeacherRelationExample = new CoachTeacherRelationExample();
            coachTeacherRelationExample.createCriteria().andCoachIdEqualTo(coach.getId());
            List<CoachTeacherRelation> coachTeacherRelations = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample);
            //根据coachTeacherRelation里面的teacherId获取teacher信息
            TeacherExample teacherExample = new TeacherExample();
            teacherExample.createCriteria().andIdEqualTo(coachTeacherRelations.get(0).getTeacherId());
            List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
            Teacher teacher = teachers.get(0);
            coachListVo.setTeacherId(teacher.getId());
            coachListVo.setNickname(teacher.getNickName());
            coachListVo.setCompany(teacher.getCompany());
            coachListVo.setWorkDuration(teacher.getWorkDuration());
            coachListVo.setAvatar(teacher.getAvatar());
            //计算出老师的综合评分以及辅导总数并存放
            CoachTeacherRelationExample coachTeacherRelationExample1 = new CoachTeacherRelationExample();
            coachTeacherRelationExample1.createCriteria().andTeacherIdEqualTo(teacher.getId());
            List<CoachTeacherRelation> coachTeacherRelations1 = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample1);
            int all = 0;
            Double score = 0.0;
            for (CoachTeacherRelation coachTeacherRelation : coachTeacherRelations1) {
                Long coachId1 = coachTeacherRelation.getCoachId();
                CoachCommentRelationExample coachCommentRelationExample = new CoachCommentRelationExample();
                coachCommentRelationExample.createCriteria().andCoachIdEqualTo(coachId1);
                List<CoachCommentRelation> coachCommentRelations = coachCommentRelationMapper.selectByExample(coachCommentRelationExample);
                if (coachCommentRelations != null && coachCommentRelations.size() > 0) {
                    //计算出一共获得的总分数，和一共授课总数，最后计算综合平均分
                    // 还没有获得用户评分以及评论的不算完成一次辅导
                    Double Sscore = 0.0;
                    int Sall = 0;
                    for (CoachCommentRelation coachCommentRelation : coachCommentRelations) {
                        CommentExample commentExample = new CommentExample();
                        commentExample.createCriteria().andIdEqualTo(coachCommentRelation.getId());
                        List<Comment> comments = commentMapper.selectByExample(commentExample);
                        Sscore += comments.get(0).getScore();
                        Sall += 1;
                    }
//                Sscore = Sscore / Sall;
                    score += Sscore;
                    all += Sall;

                    score = score / all;
                    coachListVo.setCoachNumber(all);
                    coachListVo.setScore(score);
                }
            }
            coachListVos.add(coachListVo);
        }
        return coachListVos;
    }

    @Transactional
    @Override
    public int leaveMessage(LeaveMessageParam leaveMessageParam) {
        AppointmentCoaching appointmentCoaching = new AppointmentCoaching();
        appointmentCoaching.setMessage(leaveMessageParam.getMessage());
        appointmentCoaching.setUserId(SecurityUtils.getCurrentUser().getId());
        appointmentCoaching.setCreateTime(new Date());
        if (appointmentCoachingMapper.insertSelective(appointmentCoaching) <= 0) {
            return 0;
        } else {
            CoachAppointmentRelation coachAppointmentRelation = new CoachAppointmentRelation();
            coachAppointmentRelation.setCoachId(leaveMessageParam.getCoachId());
            coachAppointmentRelation.setAppointmentId(appointmentCoaching.getId());
            if (appointmentRelationMapper.insertSelective(coachAppointmentRelation) <= 0) {
                return 0;
            }
        }
        return 1;
    }

    @Transactional
    @Override
    public int postComment(CommentPublishParam commentPublishParam) {
        Comment comment = new Comment();
        comment.setUserId(SecurityUtils.getCurrentUser().getId());
        comment.setContent(commentPublishParam.getContent());
        comment.setScore(commentPublishParam.getScore());
        comment.setCreateTime(new Date());
        if (commentMapper.insertSelective(comment) <= 0) {
            return 0;
        } else {
            CoachCommentRelation coachCommentRelation = new CoachCommentRelation();
            coachCommentRelation.setCoachId(commentPublishParam.getCoachId());
            coachCommentRelation.setCommentId(comment.getId());
            if (coachCommentRelationMapper.insertSelective(coachCommentRelation) <= 0) {
                return 0;
            }
        }
        return 1;
    }

    @Transactional
    @Override
    public int postReply(ReplyPublishParam replyPublishParam) {
        Reply reply = new Reply();
        reply.setContent(replyPublishParam.getContent());
        reply.setCreateTime(new Date());
        //根据当前用户获取教师id
        UserTeacherRelationExample userTeacherRelationExample = new UserTeacherRelationExample();
        userTeacherRelationExample.createCriteria().andUserIdEqualTo(SecurityUtils.getCurrentUser().getId());
        List<UserTeacherRelation> userTeacherRelations = userTeacherRelationMapper.selectByExample(userTeacherRelationExample);
        Long teacherId = userTeacherRelations.get(0).getTeacherId();
        reply.setTeacherId(teacherId);
        if (replyMapper.insertSelective(reply) <= 0) {
            return 0;
        } else {
            CommentReplyRelation commentReplyRelation = new CommentReplyRelation();
            commentReplyRelation.setCommentId(replyPublishParam.getCommentId());
            commentReplyRelation.setReplyId(reply.getId());
            if (commentReplyRelationMapper.insertSelective(commentReplyRelation) <= 0) {
                return 0;
            }
        }
        return 1;
    }

    @Transactional
    @Override
    public CoachingRecordListVo coachingRecords(int pageNum, int pageSize, Long teacherId, int flag) {
        CoachingRecordListVo coachingRecordListVo = new CoachingRecordListVo();
        //根据前端传来的教师id获取教师昵称
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andIdEqualTo(teacherId);
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        if (teachers != null && teachers.size() > 0) {
            String nickName = teachers.get(0).getNickName();
            coachingRecordListVo.setNickname(nickName);
            coachingRecordListVo.setAvatar(teachers.get(0).getAvatar());
        }
        List<Records> recordsList = new ArrayList<>();
        //根据当前老师所拥有辅导总数对 sql查询 进行分页
        PageHelper.startPage(pageNum, pageSize);

        CoachTeacherRelationExample coachTeacherRelationExample = new CoachTeacherRelationExample();
        coachTeacherRelationExample.createCriteria().andTeacherIdEqualTo(teacherId);
        List<CoachTeacherRelation> coachTeacherRelations = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample);
        if (coachTeacherRelations != null && coachTeacherRelations.size() > 0) {
            for (CoachTeacherRelation coachTeacherRelation : coachTeacherRelations) {

                Long coachId = coachTeacherRelation.getCoachId();
                Records records = new Records();
                //存放coachId
                records.setCoachId(coachId);
                //根据coachId获取得到辅导相关信息，并存放
                CoachExample coachExample = new CoachExample();
                coachExample.createCriteria().andIdEqualTo(coachId);
                List<Coach> coaches = coachMapper.selectByExample(coachExample);
                if (coaches != null && coaches.size() > 0) {
                    Coach coach = coaches.get(0);
                    records.setDirection(coach.getDirection());

                    if (coach.getCreateTime() == null) {
                        records.setCoachDate(null);
                    } else {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String format = simpleDateFormat.format(coach.getCreateTime());
                        records.setCoachDate(format);
                    }

                    records.setPrice(coach.getPrice());
                }
                //还差个评分score这个在comment表中
                //根据coachId获取对应的comment
                CoachCommentRelationExample coachCommentRelationExample = new CoachCommentRelationExample();
                coachCommentRelationExample.createCriteria().andCoachIdEqualTo(coachId);
                List<CoachCommentRelation> coachCommentRelations = coachCommentRelationMapper.selectByExample(coachCommentRelationExample);
                if (coachCommentRelations != null && coachCommentRelations.size() > 0) {
                    //计算每个辅导所有评分的平均值
                    Double score = 0.0;
                    int all = 0;
                    for (CoachCommentRelation coachCommentRelation : coachCommentRelations) {
                        CommentExample commentExample = new CommentExample();
                        commentExample.createCriteria().andIdEqualTo(coachCommentRelation.getId());
                        List<Comment> comments = commentMapper.selectByExample(commentExample);
                        if (comments != null && comments.size() > 0) {
                            score += comments.get(0).getScore();
                            all += 1;
                        }
                    }
                    score = score / all;
                    //将平均评分存入
                    records.setScore(score);
                }
                //创建recordsComment对象存放辅导评价相关信息
                List<RecordsComment> recordsCommentList = new ArrayList<>();
                RecordsComment recordsComment = new RecordsComment();
                for (CoachCommentRelation coachCommentRelation : coachCommentRelations) {
                    CommentExample commentExample = new CommentExample();
                    commentExample.createCriteria().andIdEqualTo(coachCommentRelation.getId());
                    List<Comment> comments = commentMapper.selectByExample(commentExample);
                    if (comments != null && comments.size() > 0) {
                        Comment comment = comments.get(0);
                        recordsComment.setCommentId(comment.getId());
                        recordsComment.setContent(comment.getContent());
                        //根据userId获取到userName
                        Long userId = comment.getUserId();
                        UserExample userExample = new UserExample();
                        userExample.createCriteria().andIdEqualTo(userId);
                        List<User> users = userMapper.selectByExample(userExample);
                        if (users != null && users.size() > 0) {
                            recordsComment.setUserName(users.get(0).getUsername());
                            recordsComment.setAvatar(users.get(0).getAvatar());
                            //根据commentId 获取 reply  并将reply相关信息存放
                            CommentReplyRelationExample commentReplyRelationExample = new CommentReplyRelationExample();
                            commentReplyRelationExample.createCriteria().andCommentIdEqualTo(comment.getId());
                            List<CommentReplyRelation> commentReplyRelations = commentReplyRelationMapper.selectByExample(commentReplyRelationExample);
                            if (commentReplyRelations != null && commentReplyRelations.size() > 0) {
                                ReplyExample replyExample = new ReplyExample();
                                replyExample.createCriteria().andIdEqualTo(commentReplyRelations.get(0).getReplyId());
                                List<Reply> replies = replyMapper.selectByExample(replyExample);
                                if (replies != null && replies.size() > 0) {
                                    recordsComment.setReply(replies.get(0).getContent());
                                }
                            }
                        }
                    }
                    //将每次的评论记录存放于列表中
                    recordsCommentList.add(recordsComment);
                }
                //将存放好的列表放入Records 最后 在将Records 放入 CoachingRecordListVo中
                records.setRecordsCommentList(recordsCommentList);
                recordsList.add(records);

                if (recordsList != null && recordsList.size() > 0) {
                    //按价格顺序排列
                    if (flag == 1) {
                        //按照价格由小到大排序
                        int minIndex;
                        for (int i = 0; i < recordsList.size() - 1; i++) {
                            minIndex = i;
                            for (int j = i + 1; j < recordsList.size(); j++) {
                                if (recordsList.get(j).getPrice().compareTo(recordsList.get(minIndex).getPrice()) <= 0) {
                                    minIndex = j;
                                }
                            }
                            //值得注意的点，list对象的排序方法 如下
                            Collections.swap(recordsList, i, minIndex);
                        }
                    }

                    //按价格逆序排列
                    if (flag == 2) {
                        //按照价格由大到小排序
                        int minIndex;
                        for (int i = 0; i < recordsList.size() - 1; i++) {
                            minIndex = i;
                            for (int j = i + 1; j < recordsList.size(); j++) {
                                if (recordsList.get(j).getPrice().compareTo(recordsList.get(minIndex).getPrice()) > 0) {
                                    minIndex = j;
                                }
                            }
                            //值得注意的点，list对象的排序方法 如下
                            Collections.swap(recordsList, i, minIndex);
                        }
                    }

                    //按时间顺序排列
                    if (flag == 3) {
                        //按照价格由小到大排序
                        int minIndex;
                        for (int i = 0; i < recordsList.size() - 1; i++) {
                            minIndex = i;
                            for (int j = i + 1; j < recordsList.size(); j++) {
                                if (recordsList.get(j).getCoachDate().compareTo(recordsList.get(minIndex).getCoachDate()) <= 0) {
                                    minIndex = j;
                                }
                            }
                            //值得注意的点，list对象的排序方法 如下
                            Collections.swap(recordsList, i, minIndex);
                        }
                    }

                    //按时间逆序排列
                    if (flag == 4) {
                        //按照价格由大到小排序
                        int minIndex;
                        for (int i = 0; i < recordsList.size() - 1; i++) {
                            minIndex = i;
                            for (int j = i + 1; j < recordsList.size(); j++) {
                                if (recordsList.get(j).getCoachDate().compareTo(recordsList.get(minIndex).getCoachDate()) > 0) {
                                    minIndex = j;
                                }
                            }
                            //值得注意的点，list对象的排序方法 如下
                            Collections.swap(recordsList, i, minIndex);
                        }
                    }
                }
                coachingRecordListVo.setRecordsList(recordsList);
            }
        }
        return coachingRecordListVo;
    }

    @Override
    public List<MyCoachListVo> getMyNotCertificatedCoachList(int pageNum, int pageSize) {
        List<MyCoachListVo> myCoachListVos = new ArrayList<>();
        //根据当前userId获取到teacherId
        UserTeacherRelationExample userTeacherRelationExample = new UserTeacherRelationExample();
        userTeacherRelationExample.createCriteria().andUserIdEqualTo(SecurityUtils.getCurrentUser().getId());
        List<UserTeacherRelation> userTeacherRelations = userTeacherRelationMapper.selectByExample(userTeacherRelationExample);
        if (userTeacherRelations != null && userTeacherRelations.size() > 0) {
            UserTeacherRelation userTeacherRelation = userTeacherRelations.get(0);
            Long teacherId = userTeacherRelation.getTeacherId();
            //分页
            PageHelper.startPage(pageNum, pageSize);
            //根据teacherId获取coach列表
            CoachTeacherRelationExample coachTeacherRelationExample = new CoachTeacherRelationExample();
            coachTeacherRelationExample.createCriteria().andTeacherIdEqualTo(teacherId);
            List<CoachTeacherRelation> coachTeacherRelations = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample);
            if (coachTeacherRelations != null && coachTeacherRelations.size() > 0) {
                for (CoachTeacherRelation coachTeacherRelation : coachTeacherRelations) {
                    CoachExample coachExample = new CoachExample();
                    coachExample.createCriteria().andIdEqualTo(coachTeacherRelation.getCoachId());
                    List<Coach> coaches = coachMapper.selectByExample(coachExample);
                    if (coaches != null && coaches.size() > 0) {
                        Coach coach = coaches.get(0);
                        //只有未认证的 辅导 才会被显示
                        if (coach.getCertification() == CoachScheduleCode.NOT_CERTIFICATED) {
                            MyCoachListVo myCoachListVo = new MyCoachListVo();
                            myCoachListVo.setCoachId(coach.getId());
                            myCoachListVo.setDirection(coach.getDirection());
                            myCoachListVo.setPrice(coach.getPrice());
                            if (coach.getCreateTime() != null) {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
                                String format = simpleDateFormat.format(coach.getCreateTime());
                                myCoachListVo.setCreateTime(format);
                            } else {
                                myCoachListVo.setCreateTime(null);
                            }
                            TeacherExample teacherExample = new TeacherExample();
                            teacherExample.createCriteria().andIdEqualTo(teacherId);
                            List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
                            if (teachers != null && teachers.size() > 0) {
                                Teacher teacher = teachers.get(0);
                                myCoachListVo.setTeacherId(teacher.getId());
                                myCoachListVo.setNickname(teacher.getNickName());
                                myCoachListVo.setCompany(teacher.getCompany());
                                myCoachListVo.setWorkDuration(teacher.getWorkDuration());
                                myCoachListVo.setAvatar(teacher.getAvatar());
                                //计算出老师的综合评分以及辅导总数并存放
                                CoachTeacherRelationExample coachTeacherRelationExample1 = new CoachTeacherRelationExample();
                                coachTeacherRelationExample1.createCriteria().andTeacherIdEqualTo(teacher.getId());
                                List<CoachTeacherRelation> coachTeacherRelations1 = coachTeacherRelationMapper.selectByExample(coachTeacherRelationExample1);
                                int all = 0;
                                Double score = 0.0;
                                for (CoachTeacherRelation coachTeacherRelation2 : coachTeacherRelations1) {

                                    Long coachId1 = coachTeacherRelation2.getCoachId();
                                    CoachCommentRelationExample coachCommentRelationExample = new CoachCommentRelationExample();
                                    coachCommentRelationExample.createCriteria().andCoachIdEqualTo(coachId1);
                                    List<CoachCommentRelation> coachCommentRelations = coachCommentRelationMapper.selectByExample(coachCommentRelationExample);
                                    if (coachCommentRelations != null && coachCommentRelations.size() > 0) {
                                        //计算出一共获得的总分数，和一共授课总数，最后计算综合平均分
                                        // 还没有获得用户评分以及评论的不算完成一次辅导
                                        Double Sscore = 0.0;
                                        int Sall = 0;
                                        for (CoachCommentRelation coachCommentRelation : coachCommentRelations) {
                                            CommentExample commentExample = new CommentExample();
                                            commentExample.createCriteria().andIdEqualTo(coachCommentRelation.getId());
                                            List<Comment> comments = commentMapper.selectByExample(commentExample);
                                            Sscore += comments.get(0).getScore();
                                            Sall += 1;
                                        }
//                Sscore = Sscore / Sall;
                                        score += Sscore;
                                        all += Sall;

                                        score = score / all;
                                        myCoachListVo.setCoachNumber(all);
                                        myCoachListVo.setScore(score);
                                    }
                                }
                            }
                            myCoachListVos.add(myCoachListVo);
                        }
                    }
                }
            }
        }
        return myCoachListVos;
    }
}


